"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : connection_id
Case Name   : 使用多线程向表中插入connection_id()函数;查看表中数据是否会重复
Create At   : 2023/07/15
Owner       :
Description :
    1.创建表
    2.多线程向表中插入数据
    3.查看表中数据是否重复
    4.清理环境;删除表
Expect      :
    1.创建表成功
    2.插入数据成功
    3.表中数据没有重复
    4.删除成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSQL import CommonSQL
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Connectionid(unittest.TestCase):

    def setUp(self):
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.test_node = Node('PrimaryDbUser')
        self.comsh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.comSQL = CommonSQL('PrimaryDbUser')
        self.table_name = 'test_table_010'
        self.db_name = 'test_database_010'

    def test_connection_id(self):
        text = '----step1:创建兼容b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.comsh.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.comsh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        create_cmd = f'drop table if exists {self.table_name};' \
                     f'create table {self.table_name}(id text,session text);'
        self.log.info(create_cmd)
        res = self.primary_sh.execut_db_sql(create_cmd, dbname=self.db_name)
        self.log.info(res)

        text = '------step2 多进程向表中插入数据;expect:成功------'
        self.log.info(text)
        insertcmd = f'insert into {self.table_name} values(connection_id(), ' \
                    f'pg_backend_pid());'
        thread_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'gsql -d {self.db_name} ' \
                     f'-p {self.test_node.db_port} -c "{insertcmd}";'
        for i in range(100):
            insertthread = ComThread(self.common.get_sh_result, args=(
                self.test_node, thread_cmd,))
            insertthread.setDaemon(True)
            insertthread.start()
            insertthread.join()

        text = '------step3:查询表中connection_id与pg_backend_pid是否一致;' \
               'expect:成功------'
        self.log.info(text)
        select_cmd = f'select * from {self.table_name} where id = session;'
        select_msg = self.comsh.execut_db_sql(select_cmd, dbname=self.db_name)
        self.log.info(select_msg)
        self.assertIn('(100 rows)', select_msg, '执行失败')

    def tearDown(self):
        text = '------step4:清理环境;expect:成功------'
        self.log.info(text)
        drop_cmd = f'clean connection to all force for ' \
                   f'database {self.db_name};' \
                   f'drop database if exists {self.db_name};'
        drop_msg = self.comsh.execut_db_sql(drop_cmd)
        self.log.info(drop_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')
